'Option Explicit

Dim doc
Set doc = application.ActiveDocument
GetUserLicence(doc)
dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
dim filename:filename = inputbox("filename == " ,filename,"sch")
main

set fso=nothing

msgbox "done!"
sub main()
	dim schfile:schfile = doc.path & "output\" & filename & ".txt"
	dim pagefile:pagefile = doc.path & "output\page.txt"
	'msgbox schfile
	dim fo,fw
	set fo = fso.opentextfile(schfile,1)
	dim freadall
	freadall= fo.readall
	fo.close
	set fo = nothing
	
	
	dim dict
	set dict = createobject("scripting.dictionary")
		
	dim s,ls,i
	dim cs,lcs,j
	dim ks,lks,k
	s = split(freadall,chr(10))
	ls = ubound(s)-lbound(s)+1
	dim selref,selpage
	for i=0 to ls-1
		if trim(s(i))<>"" then 
			cs = split(s(i),"'")
			lcs = ubound(cs)-lbound(cs)+1
			if lcs>1 then
				selref = cs(1)
				cs(0) = replace(cs(0),"): Info, String ","")
				ks = split(cs(0),"(")
				lks = ubound(ks)-lbound(ks)+1
				if lks>1 then
					selpage = ks(1)	
					if dict.Exists(selref) = false then dict.add selref,selpage
				end if
			end if		
		end if	
	next
	
	set fw = fso.createtextfile(pagefile)
	For Each okey In dict.Keys
		if okey<>"" then 
			fw.writeline(okey & chr(9) & dict(okey))	
		end if		
	Next
	fw.close
	set fw = nothing
	dict.removeall
	set dict = nothing		
end sub


'####################################################################
'####################################################################
'####################################################################
' Function to validate document
Private Function ValidateServer(docObj)
    Dim key, licenseServer, licenseToken
    key = docObj.Validate(0)
    Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
    licenseToken = licenseServer.GetToken(key)
    Set licenseServer = nothing
    'On Error Resume Next
    Err.Clear
    docObj.Validate(licenseToken)
    If Err Then
        ValidateServer = 0    
    Else 
        ValidateServer = 1
    End If
End Function
sub GetUserLicence(docObj)
	If ValidateServer(docObj) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
end sub




